package com.gosophia.oa.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;

/**
 * 判断当前登录人是否是该任务的承办人
 * @author wangxinbing
 * @since  2011-4-19
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Component("judgeCurrentUndertaker")
public class JudgeCurrentUndertaker {

	@Autowired
	private TaskManagementWebService taskManagementWebService;
	
	private Long getCurrentLoginUserId(){
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long operatorId = userDetails.getUserAccountDTO().getUserInfoDTO().getUserInfoId();
		return operatorId;
	}
	
	private boolean ifCurrentUserIsUndertaker(Long jobId,Long operatorId) throws Exception{
		JobDTO jobDto = taskManagementWebService.findJobDetailByJobId(jobId);
		if(jobDto == null || jobDto.getUndertakerDTO()== null){
			return false;
		}
		Long undertakerId = jobDto.getUndertakerDTO().getUserInfoId();
		if(operatorId.equals(undertakerId)){
			return true;
		}else{
			return false;
		}
	}
	
	public void judgeOperationAuthority(Long jobId) throws Exception{
		boolean ifMatch = ifCurrentUserIsUndertaker(jobId,getCurrentLoginUserId());
		if(!ifMatch){
			throw new Exception("权限不足，不能执行此项操作！");
		}
	}
}
